/*
 ============================================================================
 Name		: SCOClientEngine.cpp
 Author	  : William Zhang
 Version	 : 1.0
 Copyright   : all rights reserved.
 Description : CSCOClientEngine implementation
 ============================================================================
 */

#include <bttypes.h>
#include "SCOClientEngine.h"
#include "Utils.h"

CSCOClientEngine::CSCOClientEngine()
	{
	// No implementation required
	}

CSCOClientEngine::~CSCOClientEngine()
	{
	}

CSCOClientEngine* CSCOClientEngine::NewLC()
	{
	CSCOClientEngine* self = new (ELeave) CSCOClientEngine();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CSCOClientEngine* CSCOClientEngine::NewL()
	{
	CSCOClientEngine* self = CSCOClientEngine::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CSCOClientEngine::ConstructL()
	{
    TInt err;
    
    //the MAC address of remote device
    iAddr.SetReadable(_L("0022fc6d6836"));  //N79's address
    
    User::LeaveIfError(iSockServ.Connect());
    
    iPhyLink = CBluetoothPhysicalLinks::NewL(*this,iSockServ);
    iSCOLink = CBluetoothSynchronousLink::NewL(*this,iSockServ);
    
    err = iPhyLink->CreateConnection(iAddr);
    if(err != KErrNone)
        {
        //error
        User::LeaveIfError(err);
        }

	}

void CSCOClientEngine::Send(const TDesC& aMsg)
	{
	TBuf8<50> msg;
	msg.Append(aMsg);
	iSCOLink->Send(msg);
	}

//===============From MBluetoothSynchronousLinkNotifier==================
// 
// connection complete handler
//
void CSCOClientEngine::HandleSetupConnectionCompleteL(TInt aErr)
    {
    
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Setup succeed"));
//        TBuf8<50> msg;
//        msg.Append(_L("test message"));
//        iSCOLink->Send(msg);
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Setup faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }
    }

//
// new connection accept handler
//
void CSCOClientEngine::HandleAcceptConnectionCompleteL(TInt aErr)
    {
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Accept succeed"));
        //call receive function here
//        iSCOLink->Receive(iRecvData);
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Accept faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }   
    }

//
// disconnection handler
//
void CSCOClientEngine::HandleDisconnectionCompleteL(TInt aErr)
    {
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("disconnect succeed"));
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("disconnect faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }   
    }

//
// sending complete handler
//
void CSCOClientEngine::HandleSendCompleteL(TInt aErr)
    {
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Send succeed"));
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Send faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }   
    }

//
// receiving complete handler
//
void CSCOClientEngine::HandleReceiveCompleteL(TInt aErr)
    {
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Receive succeed"));
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Receive faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }   
    }


//===============From MBluetoothPhysicalLinksNotifier==================
//
// creating physical link complete 
//
void CSCOClientEngine::HandleCreateConnectionCompleteL(TInt aErr)
    {
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Physical creating succeed"));
        TInt err;
        //start listening
//        err = iSCOLink->AcceptConnection();
//        if(err != KErrNone)
//            {
//            User::LeaveIfError(err);
//            }
        
        //start connecting
        err = iSCOLink->SetupConnection(iAddr);//,EAnySCOPacket);
        if(err != KErrNone)
            {
            //error
            User::LeaveIfError(err);
            }
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Physical creating faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }   
    }

//
// physical link disconnected
//
void CSCOClientEngine::HandleDisconnectCompleteL(TInt aErr)
    {
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Physical disconnect succeed"));
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Physical disconnect faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }   
    }

//
// all physical links disconnected
//
void CSCOClientEngine::HandleDisconnectAllCompleteL(TInt aErr)
    {
    if(aErr == KErrNone)
        {
        CUtils::ShowMsg(_L("Physical disconnect all succeed"));
        }
    else
        {
        TBuf<50> buf;
        buf.AppendFormat(_L("Physical disconnect all faild,err:%d"),aErr);
        CUtils::ShowMsg(buf);
        }   
    }
